package cn.itcast.controller;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;
@RestController
@Slf4j
public class ZichangController {
    String createOrderUrl="https://8box.yunshubo.com/api/new_buy";
    Integer goodsId=1;
    private static List<IpBean> list=new ArrayList<>();
    private static List<String> tokenList=new ArrayList<>();
    @RequestMapping("/create")
    public String hello3(@RequestParam(value = "num",defaultValue = "10000") Integer num) {
        if(CollectionUtils.isEmpty(list)){
            try{
                    IpBean ipBean = new IpBean();
                    ipBean.setIpAddress("123.115.206.89");
                    ipBean.setPort(4225);
                    list.add(ipBean);
            }catch (Exception e){
                IpBean ipBean=new IpBean();
                list.add(ipBean);
            }
        }
        if(CollectionUtils.isEmpty(tokenList)){
            addToken();
        }
        for(int i=0;i<num;i++){
            try {
                for(int j=0;j<list.size();j++){
                    for(int z=0;z<tokenList.size();z++){
                        IpBean ipBean=list.get(j);
                        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ipBean.getIpAddress(), ipBean.getPort()));
                        int finalZ = z;
                        Thread.sleep(500);
                        Proxy finalProxy = proxy;
                        ThreadUtil.execAsync(() -> {
                            confireOrder(finalProxy,tokenList.get(finalZ));
                        });
                    }

                }
            } catch (Exception e) {
                log.error("#####异常####" + e.getMessage());
            }
        }
        return null;
    }
    public void confireOrder(Proxy proxy,String token){
        try{
            createOrder(proxy,token);
        }catch (Exception e){
            log.error("#####创建订单失败####"+e.getMessage()+"###"+token);
        }
    }
    public void createOrder(Proxy proxy,String token) throws InterruptedException {
        JSONObject requestJson = new JSONObject();
        requestJson.putOnce("work_id", goodsId);
        requestJson.putOnce("is_integral", false);
        requestJson.putOnce("payment", "money");
        requestJson.putOnce("transaction_password", "891223");
        requestJson.putOnce("result", "{}");
        String requestParam = requestJson.toString();
        String result = HttpRequest.post(createOrderUrl).timeout(10000).
                body(requestParam)
                .header("authorization",token)
                .execute().body();
        log.info("####支付结果######"+result+"########"+token);
    }
    public void addToken(){
        tokenList.add("Bearer 13198@a6b740907a3c3e1f05ba9bd0464987bfd3fcf44b");
    }
}
